package top.didi.study.study.servlet.notice;

import top.didi.study.study.dao.TbNoticeDAO;
import top.didi.study.study.entity.Result;
import top.didi.study.study.entity.TbNotice;
import top.didi.study.study.util.JsonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "noticemodify", urlPatterns = "/notice/modify.action")
public class ModifyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();
        resp.setContentType("application/json");
        Integer id = Integer.parseInt(req.getParameter("id"));
        String username = req.getParameter("username");
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        Integer topindex = Integer.parseInt(req.getParameter("topindex"));


        TbNotice notice = new TbNotice();
        notice.setId(id);
        notice.setUsername(username);
        notice.setTitle(title);
        notice.setContent(content);
        notice.setTopindex(topindex);
        try {
            int i = TbNoticeDAO.update(notice);
            if (i == 1) {
                Result<String> result = Result.success("修改成功");
                writer.println(JsonUtils.stringify(result));
            } else {
                Result<String> result = Result.fail("修改失败", null);
                writer.println(JsonUtils.stringify(result));
            }


        }catch (Exception e) {
            e.printStackTrace();
            Result<String> result = Result.fail("修改发生错误", null);
            writer.println(JsonUtils.stringify(result));
        }
    }
}
